<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Minim : : ADSR</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="stylesheet.css" rel="stylesheet" type="text/css">
</head>
<body> 
<center>
<table class="mainTable">
  <tr>
    <td class="header">
    	<span class="indexheader">Minim</span><br/>
    	<span class="indexnavigation">
    		<a href="index.html">core</a> | 
    		<a href="index_ugens.html">ugens</a> | 
    		<a href="index_analysis.html">analysis</a>
    	</span>
    </td>
    <td class="border-left">&nbsp;</td>
  </tr>
  
  <tr>
    <td class="classNavigation">
    	<p class="mainTextName">ADSR</p>
    	
       <p class="linkListHeader">Fields</p>
       <p class="linkList">
           
    <a href="adsr_field_audio.html" title="The default input is "audio."
  You won't need to patch to this directly, since 
  simply patching to the ADSR itself will achieve 
  the same result.">audio</a><br/>
    
	   </p>
   
    	
       <p class="linkListHeader">Methods</p>
       <p class="linkList">
           
    <a href="adsr_method_channelcount.html" title="Returns the number of channels this UGen has been configured to generate.">channelCount ( )</a><br/>
    
    <a href="adsr_method_getlastvalues.html" title="Return the last values generated by this UGen. This will most often be
 used by sub-classes when pulling data from their inputs.">getLastValues ( )</a><br/>
    
    <a href="adsr_method_noteoff.html" title="Specifies that the ADSR envelope should start the release time.">noteOff ( )</a><br/>
    
    <a href="adsr_method_noteon.html" title="Specifies that the ADSR envelope should begin.">noteOn ( )</a><br/>
    
    <a href="adsr_method_patch.html" title="Send the output of this UGen to another UGen, UGenInput, or AudioOutput.">patch ( )</a><br/>
    
    <a href="adsr_method_printinputs.html" title="Prints all inputs connected to this UGen (for debugging)">printInputs ( )</a><br/>
    
    <a href="adsr_method_samplerate.html" title="Returns the sample rate of this UGen.">sampleRate ( )</a><br/>
    
    <a href="adsr_method_setchannelcount.html" title="Let this UGen know how many channels of audio you will be asking it for.">setChannelCount ( )</a><br/>
    
    <a href="adsr_method_setparameters.html" title="Permits the changing of the ADSR parameters.">setParameters ( )</a><br/>
    
    <a href="adsr_method_setsamplerate.html" title="Set the sample rate for this UGen.">setSampleRate ( )</a><br/>
    
    <a href="adsr_method_tick.html" title="Generates one sample frame for this UGen.">tick ( )</a><br/>
    
    <a href="adsr_method_unpatch.html" title="Unpatch this UGen from an AudioOutput or other UGen.">unpatch ( )</a><br/>
    
    <a href="adsr_method_unpatchafterrelease.html" title="Tell the ADSR that it should unpatch itself from the output after the release time.">unpatchAfterRelease ( )</a><br/>
    
	   </p>
   
    </td>
    <td class="mainText border-left">
    	A UGen that plays input audio through a standard ADSR (Attack, Decay, Sustain, Release)
 envelope based on time from noteOn and noteOff.
    	<p class="memberSectionHeader">Constructors</p>
    	<pre><em>Constructor for an ADSR envelope.
 Maximum amplitude is set to 1.0.
 Attack and decay times are set to 1 sec.
 Sustain level is set to 0.0. Release time is set to 1 sec.  
 Amplitude before and after the envelope is set to 0.</em>
ADSR()
<em>Constructor for an ADSR envelope with maximum amplitude.
 Attack and decay times are set to 1 sec.
 Sustain level is set to 0.0. Release time is set to 1 sec.  
 Amplitude before and after the envelope is set to 0.</em>
ADSR(float maxAmp)
<em>Constructor for an ADSR envelope with maximum amplitude, attack Time.
 Decay time is set to 1 sec.
 Sustain level is set to 0.0. Release time is set to 1 sec.  
 Amplitude before and after the envelope is set to 0.</em>
ADSR(float maxAmp, float attTime)
<em>Constructor for an ADSR envelope with maximum amplitude, attack Time, and decay time.
 Sustain level is set to 0.0. Release time is set to 1 sec.  
 Amplitude before and after the envelope is set to 0.</em>
ADSR(float maxAmp, float attTime, float decTime)
<em>Constructor for an ADSR envelope with maximum amplitude, attack Time, decay time, and sustain level.
 Release time is set to 1 sec.  Amplitude before and after the envelope is set to 0.</em>
ADSR(float maxAmp, float attTime, float decTime, float susLvl)
<em>Constructor for an ADSR envelope with maximum amplitude, attack Time, decay time, sustain level,
 and release time.  Amplitude before and after the envelope is set to 0.</em>
ADSR(float maxAmp, float attTime, float decTime, float susLvl, float relTime)
<em>Constructor for an ADSR envelope with maximum amplitude, attack Time, decay time, sustain level,
 release time, an amplitude before the envelope.  Amplitude after the envelope is set to 0.</em>
ADSR(float maxAmp, float attTime, float decTime, float susLvl, float relTime, float befAmp)
<em>Constructor for an ADSR envelope.</em>
ADSR(float maxAmp, float attTime, float decTime, float susLvl, float relTime, float befAmp, float aftAmp)
</pre>
    	
   <p class="memberSectionHeader">Parameters</p>
   
        <span class="parameterName">maxAmp</span>&nbsp;&mdash;&nbsp;<span class="parameterDescription">float: the maximum amplitude for the envelope</span><br/>
    
        <span class="parameterName">attTime</span>&nbsp;&mdash;&nbsp;<span class="parameterDescription">float: the attack time, in seconds</span><br/>
    
        <span class="parameterName">decTime</span>&nbsp;&mdash;&nbsp;<span class="parameterDescription">float: the decay time, in seconds</span><br/>
    
        <span class="parameterName">susLvl</span>&nbsp;&mdash;&nbsp;<span class="parameterDescription">float: the percentage of the maximum amplitude to maintain after the decay completes</span><br/>
    
        <span class="parameterName">relTime</span>&nbsp;&mdash;&nbsp;<span class="parameterDescription">float: the release time, in seconds</span><br/>
    
        <span class="parameterName">befAmp</span>&nbsp;&mdash;&nbsp;<span class="parameterDescription">float: the amplitude to apply before the envelope is activated</span><br/>
    
        <span class="parameterName">aftAmp</span>&nbsp;&mdash;&nbsp;<span class="parameterDescription">float: the amplitude to apply once the envelope has completed</span><br/>
    
   
    	<p class="memberSectionHeader">Related</p>
    	
    	<p class="memberSectionHeader">Example</p>
    	<pre>/* ADSRExample&lt;br/>
   is an example of using the ADSR envelope within an instrument.
   &lt;p>
   For more information about Minim and additional features, 
   visit http://code.compartmental.net/minim/
   &lt;p>
   author: Anderson Mills&lt;br/>
   Anderson Mills's work was supported by numediart (www.numediart.org)
*/

// import everything necessary to make sound.
import ddf.minim.*;
import ddf.minim.ugens.*;

// create all of the variables that will need to be accessed in
// more than one methods (setup(), draw(), stop()).
Minim minim;
AudioOutput out;

// Every instrument must implement the Instrument interface so 
// playNote() can call the instrument's methods.
class ToneInstrument implements Instrument
{
  // create all variables that must be used througout the class
  Oscil sineOsc;
  ADSR  adsr;
  
  // constructor for this instrument
  ToneInstrument( float frequency, float amplitude )
  {    
    // create new instances of any UGen objects as necessary
    sineOsc = new Oscil( frequency, amplitude, Waves.TRIANGLE );
    adsr = new ADSR( 0.5, 0.01, 0.05, 0.5, 0.5 );
    
    // patch everything together up to the final output
    sineOsc.patch( adsr );
  }
  
  // every instrument must have a noteOn( float ) method
  void noteOn( float dur )
  {
    // turn on the ADSR
    adsr.noteOn();
    // patch to the output
    adsr.patch( out );
   }
  
  // every instrument must have a noteOff() method
  void noteOff()
  {
    // tell the ADSR to unpatch after the release is finished
    adsr.unpatchAfterRelease( out );
    // call the noteOff 
    adsr.noteOff();
  }
}

// setup is run once at the beginning
void setup()
{
  // initialize the drawing window
  size( 512, 200, P2D );

  // initialize the minim and out objects
  minim = new Minim( this );
  out = minim.getLineOut( Minim.MONO, 2048 );
  
  // pause time when adding a bunch of notes at once
  out.pauseNotes();
  
  // make four repetitions of the same pattern
  for( int i = 0; i &lt; 4; i++ )
  {
    // add some low notes
    out.playNote( 1.25 + i*2.0, 0.3, new ToneInstrument( 75, 0.49 ) );
    out.playNote( 2.50 + i*2.0, 0.3, new ToneInstrument( 75, 0.49 ) );
    
    // add some middle notes
    out.playNote( 1.75 + i*2.0, 0.3, new ToneInstrument( 175, 0.4 ) );
    out.playNote( 2.75 + i*2.0, 0.3, new ToneInstrument( 175, 0.4 ) );
    
    // add some high notes
    out.playNote( 1.25 + i*2.0, 0.3, new ToneInstrument( 3750, 0.07 ) );
    out.playNote( 1.5 + i*2.0, 0.3, new ToneInstrument( 1750, 0.02 ) );
    out.playNote( 1.75 + i*2.0, 0.3, new ToneInstrument( 3750, 0.07 ) );
    out.playNote( 2.0 + i*2.0, 0.3, new ToneInstrument( 1750, 0.02 ) );
    out.playNote( 2.25 + i*2.0, 0.3, new ToneInstrument( 3750, 0.07 ) );
    out.playNote( 2.5 + i*2.0, 0.3, new ToneInstrument( 5550, 0.09 ) );
    out.playNote( 2.75 + i*2.0, 0.3, new ToneInstrument( 3750, 0.07 ) );
  }
  // resume time after a bunch of notes are added at once
  out.resumeNotes();
  
}

// draw is run many times
void draw()
{
  // erase the window to black
  background( 0 );
  // draw using a white stroke
  stroke( 255 );
  // draw the waveforms
  for( int i = 0; i &lt; out.bufferSize() - 1; i++ )
  {
    // find the x position of each buffer value
    float x1  =  map( i, 0, out.bufferSize(), 0, width );
    float x2  =  map( i+1, 0, out.bufferSize(), 0, width );
    // draw a line from one buffer position to the next for both channels
    line( x1, 50 + out.left.get(i)*50, x2, 50 + out.left.get(i+1)*50);
    line( x1, 150 + out.right.get(i)*50, x2, 150 + out.right.get(i+1)*50);
  }  
}
</pre>
    	<p class="memberSectionHeader">Usage</p>
    	Web & Application
    </td>
  </tr>
</table>
</center>
</body>
</html>